home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / record / parent1.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  56 lines

  1. /*
  2.  * This function copies standard input to "fd" and also copies
  3.  * everything from "fd" to standard output.
  4.  * In addition, all this data that is copied could also
  5.  * be recorded in a log file, if desired.
  6.  * This is the 4.3BSD version that uses the select(2) system call.
  7.  */
  8.  
  9. #include    <sys/types.h>
  10. #include    <sys/time.h>
  11.  
  12. #define    BUFFSIZE    512
  13.  
  14. pass_all(fd, childpid)
  15. int    fd;
  16. int    childpid;
  17. {
  18.     int        maxfdp1, nfound, nread;
  19.     char        buff[BUFFSIZE];
  20.     fd_set        readmask;
  21.  
  22.     FD_ZERO(&readmask);
  23.  
  24.     for ( ; ; ) {
  25.         FD_SET(0, &readmask);
  26.         FD_SET(fd, &readmask);
  27.         maxfdp1 = fd + 1;    /* check descriptors [0..fd] */
  28.  
  29.         nfound = select(maxfdp1, &readmask, (fd_set *) 0, (fd_set *) 0,
  30.                     (struct timeval *) 0);
  31.         if (nfound < 0)
  32.             err_sys("select error");
  33.  
  34.         if (FD_ISSET(0, &readmask)) {    /* data to read on stdin */
  35.             nread = read(0, buff, BUFFSIZE);
  36.             if (nread < 0)
  37.                 err_sys("read error from stdin");
  38.             else if (nread == 0)
  39.                 break;        /* stdin EOF -> done */
  40.  
  41.             if (writen(fd, buff, nread) != nread)
  42.                 err_sys("writen error to stream pipe");
  43.         }
  44.  
  45.         if (FD_ISSET(fd, &readmask)) {
  46.                     /* data to read on stream pipe */
  47.             nread = read(fd, buff, BUFFSIZE);
  48.             if (nread <= 0)
  49.                 break;        /* error or EOF, terminate */
  50.  
  51.             if (write(1, buff, nread) != nread)
  52.                 err_sys("write error to stdout");
  53.         }
  54.     }
  55. }
  56.